head(wildschwein_BE)
## # A tibble: 6 x 8
## TierID TierName CollarID DatetimeUTC E N day
## <int> <chr> <int> <dttm> <dbl> <dbl> <chr>
## 1 1 Ueli 12272 2014-05-28 21:01:14 2570390. 1204820. Tag
## 2 1 Ueli 12272 2014-05-28 21:15:18 2570389. 1204826. Abenddaemmerung
## 3 1 Ueli 12272 2014-05-28 21:30:13 2570391. 1204821. Abenddaemmerung
## 4 1 Ueli 12272 2014-05-28 21:45:11 2570388. 1204826. Abenddaemmerung
## 5 1 Ueli 12272 2014-05-28 22:00:33 2570388. 1204819. 1Nachtviertel
## 6 1 Ueli 12272 2014-05-28 22:15:16 2570384. 1204828. 1Nachtviertel
## # … with 1 more variable: moonilumination <dbl>
head(wildschwein_metadata)
## TierID TierName CollarID Sex Gewicht Study_area
## 1 1 Ueli 12272 m 79.5 Bern
## 2 1 Ueli 12844 m 91.0 Bern
## 3 2 Sabine 12275 f 62.0 Bern
## 4 5 Nicole 12273 f 50.0 Bern
## 5 10 Caroline 13570 f 68.0 Bern
## 6 10 Caroline 13969 f 58.0 Bern
head(wildschwein_overlap_temp)
## # A tibble: 6 x 4
## TierID TierName CollarID Groups
## <int> <chr> <int> <dbl>
## 1 1 Ueli 12272 1
## 2 2 Sabine 12275 2
## 3 5 Nicole 12273 2
## 4 10 Caroline 13969 2
## 5 11 Isabelle 12274 2
## 6 16 Rosa 13972 2
head(schreck_agenda)
## # A tibble: 6 x 9
## id datum_on datum_off modus lautstaerke intervall
## <chr> <dttm> <dttm> <chr> <dbl> <dbl>
## 1 WSS_201… 2014-04-01 00:00:00 2014-06-20 00:00:00 standa… 100 15
## 2 WSS_201… 2014-07-23 00:00:00 2014-09-19 00:00:00 standa… 100 15
## 3 WSS_201… 2014-04-26 00:00:00 2014-08-08 00:00:00 standa… 50 4
## 4 WSS_201… 2014-04-26 00:00:00 2014-08-08 00:00:00 standa… 100 15
## 5 WSS_201… 2014-09-19 00:00:00 2014-10-18 00:00:00 standa… 50 20
## 6 WSS_201… 2014-05-01 00:00:00 2014-10-28 00:00:00 standa… 33 15
## # … with 3 more variables: ausrichtung_min <int>, ausrichtung_max <int>,
## # phase <dbl>
head(schreck_locations)
## # A tibble: 6 x 9
## id region flurname kultur installationsho… zaun jagddruck lat lon
## <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
## 1 WSS_2… elfing… rüti weizen&h… 1.8 nein mittel 47.5 8.10
## 2 WSS_2… elfing… steiach… weizen 1.95 nein mittel 47.5 8.12
## 3 WSS_2… elfing… schlott… weizen&s… 1.8 nein mittel 47.5 8.11
## 4 WSS_2… fanel tannenh… kartoffe… 1.8 ja gering 47.0 7.06
## 5 WSS_2… fanel tannenh… karotten… 1.8 nein gering 47.0 7.06
## 6 WSS_2… fanel fanelac… kartoffe… 1.8 nein gering 47.0 7.04
schreck_locations_ch <- schreck_locations %>% st_as_sf(coords = c("lon", "lat"), crs = CRS("+init=epsg:4326"), remove = FALSE) #%>% st_transform(crs = 2056)
schreck_locations_ch <- schreck_locations_ch %>% st_transform(crs = 2056)
schreck_locations_ch <- schreck_locations_ch %>% filter(lat < 47.2 & lon < 7.5)
coordsne <- unlist(st_geometry(schreck_locations_ch)) %>% matrix(ncol=2,byrow=TRUE) %>% as_tibble() %>% setNames(c("N","E"))
## Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
## Using compatibility `.name_repair`.
schreck_locations_ch$N <- coordsne$E
schreck_locations_ch$E <- coordsne$N
#join
schreck_locations_ch <- schreck_locations_ch %>% left_join(schreck_agenda, by=c("id"="id"))
schreck_locations_ch$wid <- c(1:25)
schreck_locations_ch <- schreck_locations_ch %>% mutate(wid=as.character(wid))
# Get common samples
head(wildschwein_BE)
## # A tibble: 6 x 8
## TierID TierName CollarID DatetimeUTC E N day
## <int> <chr> <int> <dttm> <dbl> <dbl> <chr>
## 1 1 Ueli 12272 2014-05-28 21:01:14 2570390. 1204820. Tag
## 2 1 Ueli 12272 2014-05-28 21:15:18 2570389. 1204826. Abenddaemmerung
## 3 1 Ueli 12272 2014-05-28 21:30:13 2570391. 1204821. Abenddaemmerung
## 4 1 Ueli 12272 2014-05-28 21:45:11 2570388. 1204826. Abenddaemmerung
## 5 1 Ueli 12272 2014-05-28 22:00:33 2570388. 1204819. 1Nachtviertel
## 6 1 Ueli 12272 2014-05-28 22:15:16 2570384. 1204828. 1Nachtviertel
## # … with 1 more variable: moonilumination <dbl>
head(schreck_locations_ch)
## Simple feature collection with 6 features and 20 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 2569629 ymin: 1204878 xmax: 2571106 ymax: 1207100
## Projected CRS: CH1903+ / LV95
## # A tibble: 6 x 21
## id region flurname kultur installationsho… zaun jagddruck lat lon
## <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
## 1 WSS_2… fanel tannenhof kartoffe… 1.8 ja gering 47.0 7.06
## 2 WSS_2… fanel tannenhof karotten… 1.8 nein gering 47.0 7.06
## 3 WSS_2… fanel fanelach… kartoffe… 1.8 nein gering 47.0 7.04
## 4 WSS_2… fanel fanelach… kartoffe… 1.8 nein gering 47.0 7.04
## 5 WSS_2… fanel tannenhof weizen 1.8 nein gering 47.0 7.06
## 6 WSS_2… fanel tannenhof weizen 1.8 nein gering 47.0 7.06
## # … with 12 more variables: geometry <POINT [m]>, N <dbl>, E <dbl>,
## # datum_on <dttm>, datum_off <dttm>, modus <chr>, lautstaerke <dbl>,
## # intervall <dbl>, ausrichtung_min <int>, ausrichtung_max <int>, phase <dbl>,
## # wid <chr>
sabi <- wildschwein_BE %>% filter(TierName=="Sabine")
# Filter night data
sabi <- sabi %>% filter(day != "Tag" & !is.na(day)) # only night gps
s1 <- schreck_locations_ch[5,]
sabi <- sabi %>% filter(DatetimeUTC > first(s1$datum_on) & DatetimeUTC < first(s1$datum_off))
sabi <- sabi %>% mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2))
sabi <- sabi %>% filter(dist < 400)
sabi <- sabi %>% mutate(trip = ifelse(hour(DatetimeUTC) > 16, day(DatetimeUTC)+1, day(DatetimeUTC)))
ggplot() +
geom_path(data=sabi %>% filter(trip<11), aes(x=E, y=N, color=factor(trip))) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2)
# Get example 1
wildboar_closeup <- wildschwein_BE %>%
filter(TierName=="Sabine" & DatetimeUTC > as_datetime('2015-06-09 22:30:43') & DatetimeUTC < as_datetime('2015-06-10 14:30:43')) %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>% mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60)
ggplot() +
geom_path(data=wildboar_closeup, aes(x=E, y=N, color=triptime0)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2)
# Get example 2
wildboar_closeup1 <- wildschwein_BE %>%
filter(TierName=="Sabine" & DatetimeUTC > as_datetime('2015-06-08 18:30:43') & DatetimeUTC < as_datetime('2015-06-09 14:30:43')) %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>% mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60) %>% arrange(.,triptime0)
ggplot() +
geom_path(data=wildboar_closeup1, aes(x=E, y=N, color=triptime0)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2)
# Get example 2
wildboar_closeup2 <- wildschwein_BE %>%
filter(TierName=="Sabine" & DatetimeUTC > as_datetime('2015-06-07 18:30:43') & DatetimeUTC < as_datetime('2015-06-08 14:30:43')) %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>% mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60) %>% arrange(.,triptime0)
ggplot() +
geom_path(data=wildboar_closeup2, aes(x=E, y=N, color=triptime0)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2)
# Plot
head(schreck_locations_ch)
## Simple feature collection with 6 features and 20 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: 2569629 ymin: 1204878 xmax: 2571106 ymax: 1207100
## Projected CRS: CH1903+ / LV95
## # A tibble: 6 x 21
## id region flurname kultur installationsho… zaun jagddruck lat lon
## <chr> <chr> <chr> <chr> <dbl> <chr> <chr> <dbl> <dbl>
## 1 WSS_2… fanel tannenhof kartoffe… 1.8 ja gering 47.0 7.06
## 2 WSS_2… fanel tannenhof karotten… 1.8 nein gering 47.0 7.06
## 3 WSS_2… fanel fanelach… kartoffe… 1.8 nein gering 47.0 7.04
## 4 WSS_2… fanel fanelach… kartoffe… 1.8 nein gering 47.0 7.04
## 5 WSS_2… fanel tannenhof weizen 1.8 nein gering 47.0 7.06
## 6 WSS_2… fanel tannenhof weizen 1.8 nein gering 47.0 7.06
## # … with 12 more variables: geometry <POINT [m]>, N <dbl>, E <dbl>,
## # datum_on <dttm>, datum_off <dttm>, modus <chr>, lautstaerke <dbl>,
## # intervall <dbl>, ausrichtung_min <int>, ausrichtung_max <int>, phase <dbl>,
## # wid <chr>
ggplot() +
geom_sf(data = schreck_locations_ch, color='black') +
geom_sf(data=wildschwein_BE %>% filter(TierName=="Sabi"), color="blue")
ggplot() +
geom_point(data = schreck_locations_ch, aes(x=E, y=N, color='red')) +
geom_point(data=sabi, aes(x=E, y=N,color="blue")) + ylim(1200000, 1210000) + xlim(2568000, 2578000)
## Warning: Removed 4 rows containing missing values (geom_point).
sabi %>% filter(day==10)
## # A tibble: 0 x 10
## # … with 10 variables: TierID <int>, TierName <chr>, CollarID <int>,
## # DatetimeUTC <dttm>, E <dbl>, N <dbl>, day <chr>, moonilumination <dbl>,
## # dist <dbl>, trip <dbl>
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
### data
w<-wildschwein_BE %>% mutate(date = as.Date(DatetimeUTC),
time= format(DatetimeUTC, format = "%H:%M:%S"),
hour= as.integer(format(DatetimeUTC, format = "%H"))+
as.integer(format(DatetimeUTC, format = "%M"))/60)
sl<-schreck_locations
###
s<-data.frame(schreck_locations_ch) ## I had to transform it to a data frame, otherwise something was weird about the coordinates (geometry)
###
s1<-s[!duplicated(s$id),c("id","N","E")] # list with all Schrecks and their location
# Set id to w
w <- w %>% mutate(id = row_number())
## reduce data to certain time frame
w1<-w %>%
filter (day!="Tag"&!is.na(day) &
((DatetimeUTC > as.Date("2014-05-01") & DatetimeUTC < as.Date("2014-07-04")) |
(DatetimeUTC > as.Date("2015-05-20") & DatetimeUTC < as.Date("2015-07-01")) |
(DatetimeUTC > as.Date("2016-04-04") & DatetimeUTC < as.Date("2016-10-04")) |
(DatetimeUTC > as.Date("2017-04-26") & DatetimeUTC < as.Date("2017-11-18"))))
nrow(w1)
## [1] 38039
#for(j in 1:nrow(w1)){
### look only at Schrecks that were active on that day
# s_on<-s[s$datum_on < w1[j,]$DatetimeUTC & s$datum_off > w1[j,]$DatetimeUTC,]
#if(nrow(s_on)==0)
# {w1[j,"closest_schreck"]<-"no_Schreck_on"}
#else{
#for(i in 1:nrow(s_on)){ ## calculate difference between current observation (j) and each schreck location
#s_on[i,"diff"]<-sqrt((w1[j,"N"]-s_on[i,"N"])^2+(w1[j,"E"]-s_on[i,"E"])^2)} ## add difference of current observation to location into file
#### look at the distance of the closest Schreck, only use it if less than 400m
#if(min(s_on$diff)>400){
#w1[j,"closest_schreck"]<-"no_Schreck_witin_400m"}
#else{
#w1[j,"closest_schreck"]<-s_on[s_on$diff==min(s_on$diff),"id"] ## add closest schreck to each wild boar location
#w1[j,"distance_to_closest_schreck"]<-s_on[s_on$diff==min(s_on$diff),"diff"]
#}}}
# Save data.frame to spare time
#write.csv(w1, "wildboar_loop.csv")
w1 <- read_delim("wildboar_loop.csv",",")
## Warning: Missing column names filled in: 'X1' [1]
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## X1 = col_double(),
## TierID = col_double(),
## TierName = col_character(),
## CollarID = col_double(),
## DatetimeUTC = col_datetime(format = ""),
## E = col_double(),
## N = col_double(),
## day = col_character(),
## moonilumination = col_double(),
## id = col_double(),
## closest_schreck = col_character(),
## distance_to_closest_schreck = col_double()
## )
w1 <- w1 %>% mutate(hour= as.integer(format(DatetimeUTC, format = "%H")),
tripdate= ifelse(hour < 12, as.Date(DatetimeUTC)-1, as.Date(DatetimeUTC)),
tripdate2 = as.Date(tripdate, origin="1970-01-01"))
# Merge close wildschweinschreck gps data to origin wildboar
w <- w %>% left_join(w1 %>% dplyr::select(id, closest_schreck, distance_to_closest_schreck), by="id")
w <- w %>% mutate(closest_schreck=
ifelse(is.na(closest_schreck), "no_Schreck_witin_400m", closest_schreck))
#add tripdate
w <- w %>% mutate(hour= as.integer(format(DatetimeUTC, format = "%H")),
tripdate= ifelse(hour < 12, as.Date(DatetimeUTC)-1, as.Date(DatetimeUTC)),
tripdate2 = as.Date(tripdate, origin="1970-01-01"))
w %>% dplyr::select(DatetimeUTC, tripdate2)
## # A tibble: 327,255 x 2
## DatetimeUTC tripdate2
## <dttm> <date>
## 1 2014-05-28 21:01:14 2014-05-28
## 2 2014-05-28 21:15:18 2014-05-28
## 3 2014-05-28 21:30:13 2014-05-28
## 4 2014-05-28 21:45:11 2014-05-28
## 5 2014-05-28 22:00:33 2014-05-28
## 6 2014-05-28 22:15:16 2014-05-28
## 7 2014-05-28 22:30:14 2014-05-28
## 8 2014-05-28 22:45:09 2014-05-28
## 9 2014-05-28 23:00:12 2014-05-28
## 10 2014-05-28 23:15:08 2014-05-28
## # … with 327,245 more rows
####### create trips: two hour before and two hours after first observation at "Abenddaemmerung" (i.e. presumed start of Schrecks)
#####
# split time and day in separate columns:
w1$Date <- as.Date(w1$DatetimeUTC)
## order data frame by animal and time
w1<-w1[order(w1$TierName) & order(w1$DatetimeUTC),]
#
##### Create trips: with shorest distance to schreck (each day) ########################################
# split time and day in separate columns:
w1$Date <- as.Date(w1$DatetimeUTC)
## order data frame by animal and time
w1<-w1[order(w1$TierName) & order(w1$DatetimeUTC),]
## empty column for trip id
w$tripID_dist<-"NA"
w$start_dist<-"no"
animals<-unique(w1$TierName)
for(j in 1:length(animals)){
days<-unique(w1[w1$TierName==animals[j] & !is.na(w1$TierName),]$tripdate2)
if(length(days)!=0){
for(i in 1:length(days)){
n<-w1[w1$TierName==animals[j] & w1$tripdate2==days[i],]
if(nrow(n[!is.na(n$distance_to_closest_schreck),])==0){nmin<-NA} else{
nmin<-min(n$distance_to_closest_schreck,na.rm=T)}
if(!is.na(nmin)){ ## only continue if nmin is not NA
if(nmin<=400){ ## only continue if minimal distance to schreck is less than 400m
n<-n[n$distance_to_closest_schreck==nmin & !is.na(n$distance_to_closest_schreck),]
w[w$TierName==animals[j] & (w$DatetimeUTC >= n$DatetimeUTC-2*60*60) & (w$DatetimeUTC <= n$DatetimeUTC+2*60*60),"tripID_dist"]<-paste(animals[j],i, sep="_")
w[w$TierName==animals[j] & w$DatetimeUTC==n$DatetimeUTC,"start_dist"]<-"yes"
}}}}}
## Warning in `>=.default`(w$DatetimeUTC, n$DatetimeUTC - 2 * 60 * 60): Länge des längeren Objektes
## ist kein Vielfaches der Länge des kürzeren Objektes
## Warning in `<=.default`(w$DatetimeUTC, n$DatetimeUTC + 2 * 60 * 60): Länge des längeren Objektes
## ist kein Vielfaches der Länge des kürzeren Objektes
## Warning in `==.default`(w$DatetimeUTC, n$DatetimeUTC): Länge des längeren Objektes
## ist kein Vielfaches der Länge des kürzeren Objektes
w <- w %>% group_by(TierID, tripdate2) %>% mutate(isTrip= max(tripID_dist) != 'NA',
tripIDnight = ifelse((hour < 9.1 | hour >= 17.9)&isTrip,
max(tripID_dist), NA)) %>% ungroup()
### list of all trips based on closest distance per day and two hours before and after
trips_dist<-unique(w$tripIDnight)
head(trips_dist)
## [1] "Ueli_1" NA "Ueli_2" "Ueli_3" "Ueli_4" "Ueli_5"
s1 <- schreck_locations_ch[5,]
wildboar_closeup <- wildschwein_BE %>%
filter(TierName=="Sabine" & DatetimeUTC > as_datetime('2015-06-08 18:30:43') & DatetimeUTC < as_datetime('2015-06-09 10:30:43')) %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>% mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60) %>% arrange(.,triptime0)
schreck_orientation <- s1 %>% mutate(length=lautstaerke*lautstaerke/100)
rad2deg <- function(rad) {(rad * 180) / (pi)}
deg2rad <- function(deg) {(deg * pi) / (180)}
schreck_orientation$asurichtung_mean <- (schreck_orientation$ausrichtung_max-
schreck_orientation$ausrichtung_min) / 2 +
schreck_orientation$ausrichtung_min
schreck_orientation$ausrichung_meanE = schreck_orientation$E[1] + schreck_orientation$length[1] * 2*
cos(deg2rad(360-270-(schreck_orientation$asurichtung_mean[1])))
schreck_orientation$ausrichung_meanN = schreck_orientation$N[1] + schreck_orientation$length[1] *2*
sin(deg2rad(360-270-(schreck_orientation$asurichtung_mean[1])))
schreck_orientation$ausrichung_minE = schreck_orientation$E[1] + schreck_orientation$length[1] *
cos(deg2rad(360-270-schreck_orientation$ausrichtung_min[1]))
schreck_orientation$ausrichung_minN = schreck_orientation$N[1] + schreck_orientation$length[1] *
sin(deg2rad(360-270-schreck_orientation$ausrichtung_min[1]))
schreck_orientation$ausrichung_maxE = schreck_orientation$E[1] + schreck_orientation$length[1] *
cos(deg2rad(360-270-schreck_orientation$ausrichtung_max[1]))
schreck_orientation$ausrichung_maxN = schreck_orientation$N[1] + schreck_orientation$length[1] *
sin(deg2rad(360-270-schreck_orientation$ausrichtung_max[1]))
x_coord <- c(schreck_orientation$E[1], schreck_orientation$ausrichung_minE[1],
schreck_orientation$ausrichung_meanE[1],
schreck_orientation$ausrichung_maxE[1], schreck_orientation$E[1])
y_coord <- c(schreck_orientation$N[1], schreck_orientation$ausrichung_minN[1],
schreck_orientation$ausrichung_meanN[1],
schreck_orientation$ausrichung_maxN[1], schreck_orientation$N[1])
#p = Polygon(cbind(x_coord, y_coord))
#ps = Polygons(list(p),1)
#sps = SpatialPolygons(list(ps))
#plot(sps)
#schreck_orientation$polygon[1] = sps[1]
poly <- st_polygon(list(matrix(c(x_coord, y_coord),ncol=2, byrow=FALSE))) #%>% st_geometry(poly) %>% st_set_crs(2056)
pos <- data.frame(x=x_coord, y=y_coord, id=c(1,2,3,4,5))
#schreck_orientation$polygon <- poly
ggplot() +
geom_path(data=wildboar_closeup, aes(x=E, y=N, color=triptime0)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2) +
geom_point(data = schreck_orientation, aes(x=ausrichung_meanE, y=ausrichung_meanN),
colour="brown", size=2) +
geom_polygon(data=pos, aes(x=x, y = y, fill="orange", alpha=0.4))
# Get all trips
trips <- w %>% filter(!is.na(tripIDnight)) %>% group_by(tripIDnight)
trips
## # A tibble: 6,661 x 20
## # Groups: tripIDnight [113]
## TierID TierName CollarID DatetimeUTC E N day
## <int> <chr> <int> <dttm> <dbl> <dbl> <chr>
## 1 1 Ueli 12272 2014-05-28 21:01:14 2570390. 1204820. Tag
## 2 1 Ueli 12272 2014-05-28 21:15:18 2570389. 1204826. Abenddaemmeru…
## 3 1 Ueli 12272 2014-05-28 21:30:13 2570391. 1204821. Abenddaemmeru…
## 4 1 Ueli 12272 2014-05-28 21:45:11 2570388. 1204826. Abenddaemmeru…
## 5 1 Ueli 12272 2014-05-28 22:00:33 2570388. 1204819. 1Nachtviertel
## 6 1 Ueli 12272 2014-05-28 22:15:16 2570384. 1204828. 1Nachtviertel
## 7 1 Ueli 12272 2014-05-28 22:30:14 2570393. 1204824. 1Nachtviertel
## 8 1 Ueli 12272 2014-05-28 22:45:09 2570585. 1205044. 1Nachtviertel
## 9 1 Ueli 12272 2014-05-28 23:00:12 2570576. 1205044. 1Nachtviertel
## 10 1 Ueli 12272 2014-05-28 23:15:08 2570566. 1205047. 1Nachtviertel
## # … with 6,651 more rows, and 13 more variables: moonilumination <dbl>,
## # date <date>, time <chr>, hour <int>, id <dbl>, closest_schreck <chr>,
## # distance_to_closest_schreck <dbl>, tripdate <dbl>, tripdate2 <date>,
## # tripID_dist <chr>, start_dist <chr>, isTrip <lgl>, tripIDnight <chr>
# calculate properties of scarce (approaching rate, speed)
s1 <- schreck_locations_ch[5,]
#wildboar_trip <- wildschwein_BE %>%
# filter(TierName=="Sabine" & DatetimeUTC > as_datetime('2015-06-08 19:30:43') &
# DatetimeUTC < as_datetime('2015-06-09 08:30:43')) %>%
# mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>%
# mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60) %>% arrange(.,triptime0)
wildboar_trip <- w %>% filter(tripIDnight=="Olga_6") %>% ungroup() %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>%
mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60) %>% arrange(.,triptime0)
wildboar_trip_scared <- data.frame(matrix(ncol = 21, nrow = 0))
x <- c("id", "scareEffect", "triptime0", "approachingRate", "approachingRateRelative",
"approachingRateAbsolute", "speed", "sinousity", "acceleration", "acceleration4",
"dist", "E", "N","x", "y",
"closest_schreck", "scared", "tripIDnight", "DatetimeUTC", "hour", "day")
colnames(wildboar_trip_scared) <- x
for (i in 1:length(unique(trips$tripIDnight)))
{
wildboar_trip <- trips %>% filter(tripIDnight==unique(trips$tripIDnight)[i]) %>%
mutate(dist = sqrt((first(s1$N)-N)^2+(first(s1$E)-E)^2)) %>%
mutate(triptime0 = as.numeric((DatetimeUTC - min(DatetimeUTC))) / 60,
triptimeDiff = (triptime0-lag(triptime0))*60,
distanceAbsolute = ((E- lag(E))^2 + (N-lag(N))^2)^0.5) %>% arrange(.,triptime0)
# Only take full trips
if (length(wildboar_trip$TierID) > 61)
{
s1 <- schreck_locations_ch %>% filter(id == max(wildboar_trip$closest_schreck))
# Calculate approaching rate
wildboar_trip <- wildboar_trip %>% mutate(approachingRate = lag(dist)-dist,
approachingRateAbsolute =
approachingRate / (triptimeDiff),
approachingRateRelative =
approachingRate / (distanceAbsolute))
# Calculate speed
wildboar_trip <- wildboar_trip %>%
mutate(speed = round(distanceAbsolute / triptimeDiff, 4),
acceleration = round((speed - lag(speed)) / triptimeDiff, 4),
acceleration4 = round(((speed+lead(speed))/2 - (lag(speed)+lag(speed, 2)/2)) / triptimeDiff, 4))
# Relative coordinates (trajectory)
wildboar_trip <- wildboar_trip %>% mutate(x=E-s1$E, y=N-s1$N)
coords <- data.frame(x = wildboar_trip$x,
y = wildboar_trip$y,
times = wildboar_trip$triptime0)
# Set all sinousitiy to 0
wildboar_trip$sinousity = replicate(length(coords$x), NA)
for (i in 3:(length(coords$x)-2)) {
# Create a trajectory from the coordinates
trj <- TrajFromCoords(coords[(i-2):(i+1),])
# Rescale stepsize
trj_re <- TrajRediscretize(trj, 1)
# Calculate sinousity
wildboar_trip$sinousity[i] = TrajSinuosity(trj_re, compass.direction = TRUE) %>% round(digits = 4)
}
wildboar_trip <- wildboar_trip %>% mutate(sinousity= ifelse(is.na(sinousity), 0, sinousity),
scareEffect= - normalize(approachingRateAbsolute)
+ normalize(acceleration4) - normalize(sinousity),
isScared = scareEffect > 6.5,
scared = lead(isScared))
wildboar_trip_scared <- wildboar_trip_scared %>%
rbind(wildboar_trip %>%
dplyr::select(id, scareEffect, triptime0, approachingRate, approachingRateRelative,
approachingRateAbsolute, speed, sinousity, acceleration, acceleration4, dist,
E, N, x, y, closest_schreck, scared, tripIDnight, DatetimeUTC, hour, day))
}
}
head(wildboar_trip_scared)
## # A tibble: 6 x 21
## # Groups: tripIDnight [1]
## id scareEffect[,1] triptime0 approachingRate approachingRateRelative
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 15062 NA 0 NA NA
## 2 15063 NA 15.1 -3.67 -0.715
## 3 15064 NA 30.0 5.95 0.860
## 4 15065 -0.850 45.1 -5.43 -0.997
## 5 15066 -0.656 60.6 3.41 0.457
## 6 15067 1.68 75.1 5.15 0.467
## # … with 16 more variables: approachingRateAbsolute <dbl>, speed <dbl>,
## # sinousity <dbl>, acceleration <dbl>, acceleration4 <dbl>, dist <dbl>,
## # E <dbl>, N <dbl>, x <dbl>, y <dbl>, closest_schreck <chr>,
## # scared <lgl[,1]>, tripIDnight <chr>, DatetimeUTC <dttm>, hour <int>,
## # day <chr>
wildboar_scared <- wildboar_trip_scared %>% filter(scared)
w <- w %>% left_join(wildboar_trip_scared %>%
dplyr::select(id, scareEffect, triptime0, approachingRate,
approachingRateRelative, approachingRateAbsolute, speed, sinousity,
dist, x, y, scared), by="id")
## Adding missing grouping variables: `tripIDnight`
wildboar_trip_scared_True <- wildboar_trip_scared %>% group_by(tripIDnight) %>%
mutate(tripScared = sum(scared, na.rm = TRUE)) %>%
filter(tripScared > 0)
for (u in 1:length(unique(wildboar_trip_scared_True$tripIDnight)))
{
test <- wildboar_trip_scared_True %>% filter(tripIDnight==unique(wildboar_trip_scared_True$tripIDnight)[u])
print(first(test$DatetimeUTC))
print(first(test$tripIDnight))
print(ggplot(data=test) +
geom_line( aes(x=triptime0, y=normalize(acceleration)), color="blue")+
geom_line( aes(x=triptime0, y=normalize(approachingRateAbsolute)), color="green")+
geom_line( aes(x=triptime0, y=normalize(acceleration4)), color="orange")+
geom_line( aes(x=triptime0, y=normalize(speed)), color="red")+
geom_line( aes(x=triptime0, y=normalize(sinousity)), color="purple")+
geom_line( aes(x=triptime0, y=scareEffect), color="black"))
print(ggplot() +
geom_path(data= test, aes(x=E, y=N, color=triptime0)) +
geom_point(data = schreck_locations_ch %>%filter(id == max(test$closest_schreck)),
aes(x=E, y=N), colour="black", size=2) +
geom_point(data = test %>% filter(scared), aes(x=E, y=N), color="red"))
}
## [1] "2016-06-14 18:00:10 UTC"
## [1] "Ueli_110"
## [1] "2015-06-08 18:01:16 UTC"
## [1] "Sabine_21"
## [1] "2015-06-10 18:00:20 UTC"
## [1] "Sabine_23"
## [1] "2015-06-14 18:00:44 UTC"
## [1] "Sabine_27"
## [1] "2015-06-16 18:00:44 UTC"
## [1] "Sabine_29"
## [1] "2015-06-26 18:00:15 UTC"
## [1] "Sabine_39"
## [1] "2015-05-30 18:01:04 UTC"
## [1] "Ruth_12"
## [1] "2015-06-01 18:00:44 UTC"
## [1] "Olga_14"
## [1] "2015-06-03 18:00:16 UTC"
## [1] "Olga_16"
## [1] "2015-06-05 18:00:44 UTC"
## [1] "Olga_18"
## [1] "2015-06-11 18:00:20 UTC"
## [1] "Olga_24"
## [1] "2015-06-23 18:00:12 UTC"
## [1] "Olga_36"
ggplot() +
geom_path(data=wildboar_trip, aes(x=E, y=N, color=approachingRate)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2) +
geom_point(data = wildboar_trip %>% filter(scared | isScared), aes(x=E, y=N), color="red")
ggplot() +
geom_path(data=wildboar_trip, aes(x=E, y=N, color=approachingRate)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2) +
geom_point(data = wildboar_trip %>% filter(scared | isScared), aes(x=E, y=N), color="red")
ggplot() +
geom_path(data=wildboar_trip, aes(x=E, y=N, color=speed)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2) +
geom_point(data = wildboar_trip %>% filter(scared | isScared), aes(x=E, y=N), color="red")
ggplot() +
geom_path(data=wildboar_trip, aes(x=E, y=N, color=sinousity)) +
geom_point(data = s1, aes(x=E, y=N), colour="black", size=2) +
geom_point(data = wildboar_trip %>% filter(scared | isScared), aes(x=E, y=N), color="red")